# -*- coding: utf-8 -*-

"""
Description: 处理安耐吉产品数据
"""
def split_specification(spec_str: str) -> tuple:
    """分割规格字符串为数值、计量单位和包装单位
    
    Args:
        spec_str (str): 规格字符串，例如 "1g/瓶"
        
    Returns:
        tuple: (数值, 计量单位, 包装单位)
        
    Example:
        >>> split_specification("1g/瓶")
        (1, "g", "瓶")
    """
    try:
        # 分割数值和单位部分
        value_unit, package_unit = spec_str.split("/")
        
        # 提取数值和计量单位
        value = ""
        unit = ""
        for char in value_unit:
            if char.isdigit() or char == ".":
                value += char
            else:
                unit += char
                
        return (float(value), unit.strip(), package_unit.strip())
    except Exception as e:
        raise ValueError(f"无法解析规格字符串: {spec_str}") from e


def generate_product_html(product_data: dict) -> str:
    """生成产品详情HTML

    Args:
        product_data (dict): 产品数据

    Returns:
        str: 产品详情HTML
    """
    # 生成 HTML 结构
    html = f"""
        <div class="product-container">
            <style>
                .product-container {{
                    max-width: 800px;
                    margin: 0 auto;
                    background-color: white;
                    padding: 20px;
                    border-radius: 8px;
                    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
                }}
                .product-header {{
                    border-bottom: 2px solid #eee;
                    padding-bottom: 15px;
                    margin-bottom: 20px;
                }}
                .product-title {{
                    font-size: 24px;
                    color: #333;
                    margin: 0 0 10px 0;
                }}
                .product-info {{
                    display: grid;
                    grid-template-columns: repeat(2, 1fr);
                    gap: 15px;
                }}
                .info-item {{
                    padding: 10px;
                    background-color: #f9f9f9;
                    border-radius: 4px;
                }}
                .info-label {{
                    font-weight: bold;
                    color: #666;
                }}
                .info-value {{
                    color: #333;
                }}
            </style>
            <div class="product-header">
                <h1 class="product-title">{product_data['name']}</h1>
                <div class="product-code">商品编号：{product_data['code']}</div>
            </div>
            
            <div class="product-info">
                <div class="info-item">
                    <span class="info-label">CAS号：</span>
                    <span class="info-value">{product_data['cas']}</span>
                </div>
                <div class="info-item">
                    <span class="info-label">规格：</span>
                    <span class="info-value">{product_data['specs']}</span>
                </div>
                <div class="info-item">
                    <span class="info-label">发货周期：</span>
                    <span class="info-value">{product_data['delivery_date']}天</span>
                </div>
                <div class="info-item">
                    <span class="info-label">价格：</span>
                    <span class="info-value">¥{product_data['price']}</span>
                </div>
                <div class="info-item">
                    <span class="info-label">分类：</span>
                    <span class="info-value">{product_data['category']}</span>
                </div>
            </div>

            <h2>产品属性</h2>
            <div class="product-info">
    """

    # 添加产品属性
    for key, value in product_data["product_attributes"].items():
        html += f"""
                <div class="info-item">
                    <span class="info-label">{key}：</span>
                    <span class="info-value">{value}</span>
                </div>
        """

    html += """
            </div>
        </div>
    """
    return html


def gen_upload_data(prodata: dict) -> dict:
    """生成上传数据

    Args:
        product_data (dict): 产品数据

    Returns: 上传数据"""
    spec_value, spec_unit, package_unit = split_specification(prodata["specs"])

    return {
        "code": prodata["code"],
        "name": prodata["name"],
        "brandId": 631,
        "categoryId": 216,
        "dangerousInfo": {"casNo": prodata["cas"]},
        "photos": [
            {
                "photo": "https://energy01.oss-cn-shanghai.aliyuncs.com/upload/DI/anjlogo2x.png"
            },
            {
                "photo": "https://energy01.oss-cn-shanghai.aliyuncs.com/upload/DI/anjlogo2x.png"
            },
            {
                "photo": "https://energy01.oss-cn-shanghai.aliyuncs.com/upload/DI/anjlogo2x.png"
            },
            {
                "photo": "https://energy01.oss-cn-shanghai.aliyuncs.com/upload/DI/anjlogo2x.png"
            },
            {
                "photo": "https://energy01.oss-cn-shanghai.aliyuncs.com/upload/DI/anjlogo2x.png"
            },
        ],
        "deliveryTime": 103,
        "desc": generate_product_html(prodata),
        "status": 3,
        "skuInfos": [
            {
                "specification": {
                    "packingSpecificationValue": spec_value,
                    "packingSpecificationUnit": spec_unit,
                    "minSpecificationValue": "",
                    "minSpecificationUnit": "",
                },
                "unit": "瓶",
                "code": prodata["code"],
                "price": float(prodata["price"]) / 0.95 * 0.9,
                # "directoryPrice": 0,
                "stock": 999,
                "carryFeeTemplateId": 7114,
            },
        ],
    }
